package com.portal.service.impl;

import com.github.pagehelper.PageHelper;
import com.portal.service.StudentService;
import com.test.mapper.GradeMapper;
import com.test.mapper.StudentMapper;
import com.test.model.Grade;
import com.test.model.GradeExample;
import com.test.model.Student;
import com.test.model.StudentExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private GradeMapper gradeMapper;
    @Override
    public List<Student> getStudentList(int pageSize, int pageNum) {

        StudentExample studentExample = new StudentExample();
        PageHelper.startPage(pageNum,pageSize);
        List<Student> students = studentMapper.selectByExample(studentExample);

        return students;
    }

    @Override
    public Grade getGradeByStudentId(Long studentId) {
        Student student = studentMapper.selectByPrimaryKey(studentId.intValue());
        Grade grade = null;
        if(student !=null){
            grade = gradeMapper.selectByPrimaryKey(student.getGradeId());
        }


        return grade;
    }

    @Override
    public List<Student> getStudentByGradeName(String gradeName) {
        GradeExample gradeExample = new GradeExample();
        gradeExample.createCriteria().andGradeNameEqualTo(gradeName);
        List<Grade> grades = gradeMapper.selectByExample(gradeExample);
        if(grades.size()>0){
            StudentExample studentExample = new StudentExample();
            studentExample.createCriteria().andGradeIdEqualTo(grades.get(0).getId());
            List<Student> students = studentMapper.selectByExample(studentExample);
            return students;
        }
        return null;
    }
}
